package com.platform.api.film.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.platform.api.bzproject.service.IBzTalentService;
import com.platform.api.bzproject.vo.TalentVo;
import com.platform.api.film.model.FilmShort;
import com.platform.api.film.service.FilmShortService;
import com.platform.common.core.Result;
import com.platform.common.util.PageParam;
import com.platform.security.util.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 短剧电影表
 *
 * @author guoshao
 * @email 2549789059@qq.com
 * @date 2023-09-03 07:52:34
 */

@RestController
@RequestMapping("{appid}/film/filmShort")
public class FilmShortController {
    @Autowired
    private FilmShortService filmShortService;

    @Autowired
    private IBzTalentService iBzTalentService;

    /**
     * 分页获取
     */
    @GetMapping("u/myPage")
    public Result page(FilmShort filmShort, PageParam<FilmShort> page) {
        filmShort.setUserId(SecurityUtils.getUser().getUserId());
        IPage<FilmShort> filmShorts = filmShortService.page(page, Wrappers.query(filmShort).orderByDesc("id"));
        return Result.success(filmShorts);
    }


    /**
     * 查询我的电影
     */
    @GetMapping("u/list")
    public Result list() {
        FilmShort filmShort = new FilmShort();
        filmShort.setUserId(SecurityUtils.getUser().getUserId());
        List<FilmShort> list = filmShortService.list(new LambdaQueryWrapper<FilmShort>()
                .eq(FilmShort::getStatus,2)
                .eq(FilmShort::getUserId,SecurityUtils.getUserId())
                .select(FilmShort::getId,FilmShort::getName)
                .orderByDesc(FilmShort::getId));
        return Result.success(list);
    }

    /**
     * 保存信息
     */
    @PostMapping("u/save")
    public Result save(@RequestBody FilmShort filmShort) {
        //暂时不审核，直接通过
        filmShort.setStatus(2);
        return Result.bool(filmShortService.saveShort(filmShort));
    }


    /**
     * 获取信息
     */
    @GetMapping("u/getById/{id}")
    public Result getById(@PathVariable("id") Long id) {
        FilmShort filmShort = filmShortService.getById(id);
        return Result.success(filmShort);
    }

    /**
     * 删除信息
     */
    @GetMapping("u/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        FilmShort filmShort = filmShortService.getById(id);
        if(Objects.nonNull(filmShort)){
            if(SecurityUtils.getUserId().equals(filmShort.getUserId())){
                return Result.bool(filmShortService.removeById(id));
            }
        }

        return Result.failure("删除失败");
    }

    @PostMapping("u/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids){

        if(null == ids){
            return Result.failure("请选择要删除的数据");
        }

        List<FilmShort> list = filmShortService.list(new LambdaQueryWrapper<FilmShort>()
                .eq(FilmShort::getUserId,SecurityUtils.getUser().getUserId()).in(FilmShort::getId, ids));

        List<Long> rIds = list.stream().map(FilmShort::getId).collect(Collectors.toList());

        return Result.bool(filmShortService.removeByIds(rIds));
    }

    @PostMapping("u/setClassData/{filmCategoryId}")
    public Result setClassData(@RequestBody List<Long> ids,@PathVariable Long filmCategoryId){

        if(null == ids){
            return Result.failure("请选择要设置的数据");
        }
        Collection<FilmShort> entityList = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            FilmShort filmShort = new FilmShort();
            filmShort.setId(ids.get(i));
            filmShort.setFilmCategoryId(filmCategoryId);
            entityList.add(filmShort);
        }

        return Result.bool(filmShortService.updateBatchById(entityList));
    }


    /**
     * 用户端-分页获取
     */
    @GetMapping("filmPage")
    public Result filmPage(String name,Integer filmCategoryId, PageParam<FilmShort> page) {
        String userId;
        //TODO 查询口令
        if(StringUtils.isNotBlank(name)){
            TalentVo talentVo = new TalentVo();
            talentVo.setWord(name);
            TalentVo talent = iBzTalentService.selectByWord(talentVo);
            userId = Objects.nonNull(talent) ? talent.getUserid() : null;
        } else {
            userId = null;
        }

        LambdaQueryWrapper<FilmShort> lambdaQuery = new LambdaQueryWrapper<FilmShort>();

        lambdaQuery.and(StringUtils.isNotBlank(name), qr ->
                qr.eq(Objects.nonNull(userId),FilmShort::getUserId, userId).
                or().
                like(StringUtils.isNotBlank(name),FilmShort::getName, name)
        );
        lambdaQuery.eq(FilmShort::getStatus, 2).orderByDesc(FilmShort::getId)
        .eq(Objects.nonNull(filmCategoryId), FilmShort::getFilmCategoryId, filmCategoryId)
        .select(FilmShort::getName, FilmShort::getId, FilmShort::getCoverImg);

        IPage<FilmShort> filmShorts = filmShortService.page(page, lambdaQuery);
        return Result.success(filmShorts);
    }
}
